<?php
/**
  * Mar 4, 2008
  *
  * Coded by: Ropu
  *
  */


class HackerDAO {

  static public function getById(Hacker &$hacker) {
    $db = DB::getDB();
    $openSocials = array();

    $sql ="SELECT   h.nickname, 
                    h.hackathon_id,
                    h.extra_info ,
                    h.cv,
                    h.name,
                    h.email,
                    h.picture,
                    h.work,
                    os.id os_id,
                    os.network,
                    os.date
                  FROM hacker h 
                          INNER JOIN opensocial os ON os.hacker_id = h.id
                  WHERE h.id = " . (int) $hacker->getId(); 

    $res = $db->query($sql);
    if(!$res->num_rows) {
      throw new Exception('No hacker Found...', 10);
    }
    $row = $res->fetch_assoc();
    $hacker->setNickname($row['nickname']);
    $hacker->setHackathon($row['hackathon_id']);
    $hacker->setExtraInfo($row['extra_info']);
    $hacker->setCV($row['cv']);
    $hacker->setName($row['name']);
    $hacker->setEmail($row['email']);
    $hacker->setPicutre($row['picture']);
    $hacker->setWork($row['work']);
    do {
       $openSocials[] = new OpenSocial($row['os_id'], $row['network'], $row['date']);
    } while($row = $res->fetch_assoc());
    $hacker->setOpenSocial($openSocials);

    
//    if($stmt = $db->prepare($sql)) {
//      echo $hacker_id = $hacker->getId();
////      $stmt->bind_param("i", $hacker_id);
//      $stmt->execute();
//      if(Log::medium($stmt->error)) {
//        $error = $stmt->error;
//        $stmt->close();
//        throw new DBException($error);
//      }
//      
////      if(!$stmt->num_rows) {
////        $stmt->close();
////        throw new Exception('No hacker Found...', 10);
////      }
//      $stmt->bind_result($hacker_id,
//                        $nickname,
//                        $hackathon_id,
//                        $extra_info//,
////                        $os_id,
////                        $network,
////                        $date
//                        );
//      while($stmt->fetch()) {
//        $openSocials[] = new OpenSocial($os_id, $network, $date);
//      }
//      $stmt->close();
//    }
    return true;
  }
  
  static public function insert() {
    $db = DB::getDB();
    $sql = "INSERT INTO hacker (id) VALUES (null)";
    $db->query($sql);
    return $db->insert_id;
  }
  

  public static function save(Hacker &$hacker) {
    $db = DB::getDB();
    if ($stmt = $db->prepare("UPDATE  `hacker` SET 
                                      `nickname` = ?,
                                      `hackathon_id` = ?,
                                      `extra_info` = ?,
                                      `cv` = ?,
                                      `name` = ?,
                                      `email` = ?,
                                      `picture` = ?,
                                      `work` = ?
                                      WHERE `id` = ? LIMIT 1")) {
      $stmt->bind_param("sissssssi", $hacker->getNickname(), 
                                $hacker->getHackathonId(), 
                                $hacker->getExtraInfo(),
                                $hacker->getCV(),
                                $hacker->getName(),
                                $hacker->getEmail(),
                                $hacker->getPicture(),
                                $hacker->getWork(), 
                                $hacker->getId());
              
      $stmt->execute();
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new DBException($error);
      }
      $stmt->close();
    }
    return $hacker->getId();
  }
}